package com.aiyige.page.globalsearch.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.aiyige.page.globalsearch.SearchAllPage;
import com.aiyige.page.globalsearch.SearchDetailPage;
import com.aiyige.page.globalsearch.event.EventGlobalSearch;
import com.aiyige.page.globalsearch.util.SearchPageInterface;
import com.aiyige.page.globalsearch.util.SearchUtil;

import org.greenrobot.eventbus.EventBus;

import java.util.LinkedList;
import java.util.List;

public class GlobalSearchPagerAdatper extends FragmentStatePagerAdapter {
    List<Fragment> pageList = new LinkedList<>();

    public GlobalSearchPagerAdatper(FragmentManager fm) {
        super(fm);
        pageList.add(SearchAllPage.newInstance());
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_USER));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_INFO));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_PHOTO));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_VIDEO));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_SPECIAL_TOPIC));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_LEARN_VIDEO));
        pageList.add(SearchDetailPage.newInstance(SearchUtil.PAGE_TYPE_SEARCH_MAJOR_COURSE));
    }

    @Override
    public Fragment getItem(int position) {
        return pageList.get(position);
    }

    @Override
    public int getCount() {
        return pageList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return ((SearchPageInterface) pageList.get(position)).pageTitle();
    }


    public void postSearch(String searchWord) {
        for (Fragment fragment : pageList) {
            ((SearchPageInterface) fragment).searchWord(searchWord);
        }
        EventBus.getDefault().post(EventGlobalSearch.newBuilder()
                .type(EventGlobalSearch.TYPE_DO_SEARCH)
                .build());
    }
}
